package com.demo.controller;

import com.demo.entity.ProductRecord;
import com.demo.entity.User;
import com.demo.repository.ProductRecordRepo;
import com.demo.repository.UserRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by lenovo on 2017/6/22.
 */
@RestController
public class UserProductRecordController {
    @Autowired(required=true)
    UserRepo userRepo;

    @Autowired(required=true)
    ProductRecordRepo productRecordRepo;

    @RequestMapping(value = "/user-product-record")

    public List<ProductRecord> findUserProductRecord(HttpSession session,@RequestParam("pageIndex")  int pageIndex){

        Object userObj = session.getAttribute("user");
        User user=null;
        if (null!=userObj) {
            user=(User) userObj;
        }else{
            user=userRepo.findOne(1);
        }
        //pageIndex=0;
        Pageable pageable = new PageRequest(pageIndex,10);

        System.out.println("当前页面:"+pageIndex);

        List<ProductRecord> records=productRecordRepo.findByUserIdOrderByCreateTimeDesc(user.getId(),pageable);
        //return new ArrayList<ProductRecord>();
        return records;
    }
}
